seo

Apache – Redirect an Index File to Your Domain Without Looping

The saga of redirecting all requests to https://moz.com/index.php to point to https://moz.com/ has finally come to an end. We now practice what we preach. For those who are not familiar, it’s important to have all duplicate content point to a single, “canonical” edition of the content. Requests to index.php and requests to www.seomoz.org/ have the same content even though they have different URLs. More information on this is available in the beginner’s guide to SEO – Canonical Issues and Duplicate Content.

The problem I was having with doing a simple 301 redirect from index.php to the canonical URL is that apache treats both those requests as the same thing. A request to www.seomoz.org is treated as a request to index.php because they’re both using the same file. This resulted in apache infinitely redirecting index.php to itself.

In my search for a solution, I found this solution on webmasterworld that apparently works for some, but did not work for me.

This is the solution that did end up working:
Note, my solution requires your index file to be using php. It may work for other languages, but they are not documented here.

  1. Copy index.php to another filename that is not set as a DirectoryIndex by apache, for this example we’ll be using sitehome.php
  2. Create an apache DirectoryIndex directive for your document root. Set it to sitehome.php. Do not set the directive on a server-wide level otherwise it may cause problems with other folders that still need to use index.php as a directory index.
    Put this in an .htaccess file in your document root: DirectoryIndex sitehome.php Or if you aren’t using per-directory context files, put this in your httpd.conf

      DirectoryIndex sitehome.php
  3. Clear out the contents of your original index.php file. Insert this line of code:

That should do it. Basically we’re just making it so index.php is not a directory index file.  This forces sitehome.php to gets read when someone types in the canonical url (https://moz.com).   Any requests to index.php from old links are redirected while avoiding an infinite loop.

For more information on using 301 redirects, such as for redirect requests to http://example.com to http://www.example.com, read my guide to applying 301 redirects in apache.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button